home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / linux-bo / etherboo.000 / etherboo / etherboot-2.0 / netboot-freebsd / 3c509.c next >
C/C++ Source or Header  |  1996-01-19  |  14KB  |  553 lines

  1. /**************************************************************************
  2. NETBOOT -  BOOTP/TFTP Bootstrap Program
  3.  
  4. Author: Martin Renters.
  5.   Date: Mar 22 1995
  6.  
  7.  This code is based heavily on David Greenman's if_ed.c driver and
  8.   Andres Vega Garcia's if_ep.c driver.
  9.  
  10.  Copyright (C) 1993-1994, David Greenman, Martin Renters.
  11.  Copyright (C) 1993-1995, Andres Vega Garcia.
  12.  Copyright (C) 1995, Serge Babkin.
  13.   This software may be used, modified, copied, distributed, and sold, in
  14.   both source and binary form provided that the above copyright and these
  15.   terms are retained. Under no circumstances are the authors responsible for
  16.   the proper functioning of this software, nor do the authors assume any
  17.   responsibility for damages incurred with its use.
  18.  
  19. 3c509 support added by Serge Babkin (babkin@hq.icb.chel.su)
  20.  
  21. $Id: 3c509.c,v 1.2 1995/05/30 07:58:52 rgrimes Exp $
  22.  
  23. ***************************************************************************/
  24.  
  25. /* #define EDEBUG */
  26.  
  27. #include "netboot.h"
  28. #include "3c509.h"
  29.  
  30. short aui;
  31. char bnc=0, utp=0; /* for 3C509 */
  32. unsigned short eth_nic_base;
  33. unsigned short eth_asic_base;
  34. unsigned short eth_base;
  35. unsigned char  eth_tx_start;
  36. unsigned char  eth_laar;
  37. unsigned char  eth_flags;
  38. unsigned char  eth_vendor;
  39. unsigned char  eth_memsize;
  40. unsigned char  *eth_bmem;
  41. unsigned char  *eth_rmem;
  42. unsigned char  *eth_node_addr;
  43.  
  44. static send_ID_sequence();
  45. static get_eeprom_data();
  46. static get_e();
  47.  
  48. /**************************************************************************
  49. The following two variables are used externally
  50. ***************************************************************************/
  51. char packet[ETH_MAX_PACKET];
  52. int  packetlen;
  53.  
  54. /*********************** Name of driver *********************************/
  55.  
  56. char eth_driver[]="ep0";
  57.  
  58. /**************************************************************************
  59. ETH_PROBE - Look for an adapter
  60. ***************************************************************************/
  61. int eth_probe()
  62. {
  63.     /* common variables */
  64.     int i;
  65.     /* variables for 3C509 */
  66.     int data, j, io_base, id_port = EP_ID_PORT;
  67.     u_short k;
  68.     int ep_current_tag = EP_LAST_TAG + 1;
  69.     short *p;
  70.  
  71.     eth_vendor = VENDOR_NONE;
  72.  
  73.     /*********************************************************
  74.             Search for 3Com 509 card
  75.     ***********************************************************/
  76.  
  77.     /* Look for the EISA boards, leave them activated */
  78.     /* search for the first card, ignore all others */
  79.     for(j = 1; j < 16 && eth_vendor==VENDOR_NONE ; j++) {
  80.         io_base = (j * EP_EISA_START) | EP_EISA_W0;
  81.         if (inw(io_base + EP_W0_MFG_ID) != MFG_ID)
  82.             continue;
  83.  
  84.         /* we must found 0x1f if the board is EISA configurated */
  85.         if ((inw(io_base + EP_W0_ADDRESS_CFG) & 0x1f) != 0x1f)
  86.             continue;
  87.  
  88.         /* Reset and Enable the card */
  89.         outb(io_base + EP_W0_CONFIG_CTRL, W0_P4_CMD_RESET_ADAPTER);
  90.         DELAY(1000); /* we must wait at least 1 ms */
  91.         outb(io_base + EP_W0_CONFIG_CTRL, W0_P4_CMD_ENABLE_ADAPTER);
  92.  
  93.         /*
  94.              * Once activated, all the registers are mapped in the range
  95.              * x000 - x00F, where x is the slot number.
  96.              */
  97.         eth_base = j * EP_EISA_START;
  98.         eth_vendor = VENDOR_3C509;
  99.     }
  100.     ep_current_tag--;
  101.  
  102.     /* Look for the ISA boards. Init and leave them actived */
  103.     /* search for the first card, ignore all others */
  104.     outb(id_port, 0xc0);    /* Global reset */
  105.     DELAY(1000);
  106.     for (i = 0; i < EP_MAX_BOARDS && eth_vendor==VENDOR_NONE; i++) {
  107.         outb(id_port, 0);
  108.         outb(id_port, 0);
  109.         send_ID_sequence(id_port);
  110.  
  111.         data = get_eeprom_data(id_port, EEPROM_MFG_ID);
  112.         if (data != MFG_ID)
  113.             break;
  114.  
  115.         /* resolve contention using the Ethernet address */
  116.         for (j = 0; j < 3; j++)
  117.             data = get_eeprom_data(id_port, j);
  118.  
  119.         eth_base =
  120.             (get_eeprom_data(id_port, EEPROM_ADDR_CFG) & 0x1f) * 0x10 + 0x200;
  121.         outb(id_port, ep_current_tag);    /* tags board */
  122.         outb(id_port, ACTIVATE_ADAPTER_TO_CONFIG);
  123.         eth_vendor = VENDOR_3C509;
  124.         ep_current_tag--;
  125.     }
  126.  
  127.     if(eth_vendor != VENDOR_3C509)
  128.         goto no3c509;
  129.  
  130.     /*
  131.     * The iobase was found and MFG_ID was 0x6d50. PROD_ID should be
  132.     * 0x9[0-f]50
  133.     */
  134.     GO_WINDOW(0);
  135.     k = get_e(EEPROM_PROD_ID);
  136.     if ((k & 0xf0ff) != (PROD_ID & 0xf0ff))
  137.         goto no3c509;
  138.  
  139.     if(eth_base >= EP_EISA_START) {
  140.         printf("3C5x9 board on EISA at 0x%x - ",eth_base);
  141.     } else {
  142.         printf("3C5x9 board on ISA at 0x%x - ",eth_base);
  143.     }
  144.  
  145.     /* test for presence of connectors */
  146.     i = inw(IS_BASE + EP_W0_CONFIG_CTRL);
  147.     j = inw(IS_BASE + EP_W0_ADDRESS_CFG) >> 14;
  148.  
  149.     switch(j) {
  150.         case 0:
  151.             if(i & IS_UTP) {
  152.                 printf("10baseT\r\n");
  153.                 utp=1;
  154.                 }
  155.             else {
  156.                 printf("10baseT not present\r\n");
  157.                 eth_vendor=VENDOR_NONE;
  158.                 goto no3c509;
  159.                 }
  160.  
  161.             break;
  162.         case 1:
  163.             if(i & IS_AUI)
  164.                 printf("10base5\r\n");
  165.             else {
  166.                 printf("10base5 not present\r\n");
  167.                 eth_vendor=VENDOR_NONE;
  168.                 goto no3c509;
  169.                 }
  170.  
  171.             break;
  172.         case 3:
  173.             if(i & IS_BNC) {
  174.                 printf("10base2\r\n");
  175.                 bnc=1;
  176.                 }
  177.             else {
  178.                 printf("10base2 not present\r\n");
  179.                 eth_vendor=VENDOR_NONE;
  180.                 goto no3c509;
  181.                 }
  182.  
  183.             break;
  184.         default:
  185.             printf("unknown connector\r\n");
  186.             eth_vendor=VENDOR_NONE;
  187.             goto no3c509;
  188.         }
  189.     /*
  190.     * Read the station address from the eeprom
  191.     */
  192.     p = (u_short *) arptable[ARP_CLIENT].node;
  193.     for (i = 0; i < 3; i++) {
  194.         GO_WINDOW(0);
  195.         p[i] = htons(get_e(i));
  196.         GO_WINDOW(2);
  197.         outw(BASE + EP_W2_ADDR_0 + (i * 2), ntohs(p[i]));
  198.     }
  199.  
  200.     printf("Ethernet address: ");
  201.     for(i=0; i<5; i++) {
  202.         printf("%b:",arptable[ARP_CLIENT].node[i]);
  203.     }
  204.     printf("%b\n",arptable[ARP_CLIENT].node[i]);
  205.  
  206.     eth_node_addr = arptable[ARP_CLIENT].node;
  207.     eth_reset();
  208.     return eth_vendor;
  209. no3c509:
  210.     eth_vendor = VENDOR_NONE;
  211.     return VENDOR_NONE;
  212. }
  213.  
  214. /**************************************************************************
  215. ETH_RESET - Reset adapter
  216. ***************************************************************************/
  217. void eth_reset()
  218. {
  219.     int i;
  220.  
  221.     /***********************************************************
  222.             Reset 3Com 509 card
  223.     *************************************************************/
  224.  
  225.     if(eth_vendor != VENDOR_3C509)
  226.         return;
  227.  
  228.     /* stop card */
  229.     outw(BASE + EP_COMMAND, RX_DISABLE);
  230.     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
  231.     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
  232.     outw(BASE + EP_COMMAND, TX_DISABLE);
  233.     outw(BASE + EP_COMMAND, STOP_TRANSCEIVER);
  234.     outw(BASE + EP_COMMAND, RX_RESET);
  235.     outw(BASE + EP_COMMAND, TX_RESET);
  236.     outw(BASE + EP_COMMAND, C_INTR_LATCH);
  237.     outw(BASE + EP_COMMAND, SET_RD_0_MASK);
  238.     outw(BASE + EP_COMMAND, SET_INTR_MASK);
  239.     outw(BASE + EP_COMMAND, SET_RX_FILTER);
  240.  
  241.     /*
  242.     * initialize card
  243.     */
  244.     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
  245.  
  246.     GO_WINDOW(0);
  247.  
  248.     /* Disable the card */
  249.     outw(BASE + EP_W0_CONFIG_CTRL, 0);
  250.  
  251.     /* Configure IRQ to none */
  252.     outw(BASE + EP_W0_RESOURCE_CFG, SET_IRQ(0));
  253.  
  254.     /* Enable the card */
  255.     outw(BASE + EP_W0_CONFIG_CTRL, ENABLE_DRQ_IRQ);
  256.  
  257.     GO_WINDOW(2);
  258.  
  259.     /* Reload the ether_addr. */
  260.     for (i = 0; i < ETHER_ADDR_SIZE; i++)
  261.         outb(BASE + EP_W2_ADDR_0 + i, arptable[ARP_CLIENT].node[i]);
  262.  
  263.     outw(BASE + EP_COMMAND, RX_RESET);
  264.     outw(BASE + EP_COMMAND, TX_RESET);
  265.  
  266.     /* Window 1 is operating window */
  267.     GO_WINDOW(1);
  268.     for (i = 0; i < 31; i++)
  269.         inb(BASE + EP_W1_TX_STATUS);
  270.  
  271.     /* get rid of stray intr's */
  272.     outw(BASE + EP_COMMAND, ACK_INTR | 0xff);
  273.  
  274.     outw(BASE + EP_COMMAND, SET_RD_0_MASK | S_5_INTS);
  275.  
  276.     outw(BASE + EP_COMMAND, SET_INTR_MASK);
  277.  
  278.     outw(BASE + EP_COMMAND, SET_RX_FILTER | FIL_INDIVIDUAL |
  279.         FIL_BRDCST);
  280.  
  281.     /* configure BNC */
  282.     if(bnc) {
  283.         outw(BASE + EP_COMMAND, START_TRANSCEIVER);
  284.         DELAY(1000);
  285.         }
  286.     /* configure UTP */
  287.     if(utp) {
  288.         GO_WINDOW(4);
  289.         outw(BASE + EP_W4_MEDIA_TYPE, ENABLE_UTP);
  290.         GO_WINDOW(1);
  291.         }
  292.  
  293.     /* start tranciever and receiver */
  294.     outw(BASE + EP_COMMAND, RX_ENABLE);
  295.     outw(BASE + EP_COMMAND, TX_ENABLE);
  296.  
  297.     /* set early threshold for minimal packet length */
  298.     outw(BASE + EP_COMMAND, SET_RX_EARLY_THRESH | ETH_MIN_PACKET);
  299.  
  300.     outw(BASE + EP_COMMAND, SET_TX_START_THRESH | 16);
  301. }
  302.  
  303. /**************************************************************************
  304. ETH_TRANSMIT - Transmit a frame
  305. ***************************************************************************/
  306. static char padmap[] = {
  307.     0, 3, 2, 1};
  308.  
  309. void eth_transmit(d,t,s,p)
  310. char *d;            /* Destination */
  311. unsigned int t;            /* Type */
  312. unsigned int s;            /* size */
  313. char *p;            /* Packet */
  314. {
  315.     register u_int len;
  316.     int pad;
  317.     int status;
  318.  
  319.     if(eth_vendor != VENDOR_3C509)
  320.         return;
  321.  
  322. #ifdef EDEBUG
  323.     printf("{l=%d,t=%x}",s+ETHER_HDR_SIZE,t);
  324. #endif
  325.  
  326.     /* swap bytes of type */
  327.     t= htons(t);
  328.  
  329.     len=s+ETHER_HDR_SIZE; /* actual length of packet */
  330.     pad = padmap[len & 3];
  331.  
  332.     /*
  333.     * The 3c509 automatically pads short packets to minimum ethernet length,
  334.     * but we drop packets that are too large. Perhaps we should truncate
  335.     * them instead?
  336.     */
  337.     if (len + pad > ETH_MAX_PACKET) {
  338.         return;
  339.     }
  340.  
  341.     /* drop acknowledgements */
  342.     while(( status=inb(BASE + EP_W1_TX_STATUS) )& TXS_COMPLETE ) {
  343.         if(status & (TXS_UNDERRUN|TXS_MAX_COLLISION|TXS_STATUS_OVERFLOW)) {
  344.             outw(BASE + EP_COMMAND, TX_RESET);
  345.             outw(BASE + EP_COMMAND, TX_ENABLE);
  346.         }
  347.  
  348.         outb(BASE + EP_W1_TX_STATUS, 0x0);
  349.     }
  350.  
  351.     while (inw(BASE + EP_W1_FREE_TX) < len + pad + 4) {
  352.         /* no room in FIFO */
  353.     }
  354.  
  355.     outw(BASE + EP_W1_TX_PIO_WR_1, len);
  356.     outw(BASE + EP_W1_TX_PIO_WR_1, 0x0);    /* Second dword meaningless */
  357.  
  358.     /* write packet */
  359.     outsw(BASE + EP_W1_TX_PIO_WR_1, d, ETHER_ADDR_SIZE/2);
  360.     outsw(BASE + EP_W1_TX_PIO_WR_1, eth_node_addr, ETHER_ADDR_SIZE/2);
  361.     outw(BASE + EP_W1_TX_PIO_WR_1, t);
  362.     outsw(BASE + EP_W1_TX_PIO_WR_1, p, s / 2);
  363.     if (s & 1)
  364.         outb(BASE + EP_W1_TX_PIO_WR_1, *(p+s - 1));
  365.  
  366.     while (pad--)
  367.         outb(BASE + EP_W1_TX_PIO_WR_1, 0);    /* Padding */
  368.  
  369.     /* timeout after sending */
  370.     DELAY(1000);
  371. }
  372.  
  373. /**************************************************************************
  374. ETH_POLL - Wait for a frame
  375. ***************************************************************************/
  376. int eth_poll()
  377. {
  378.     /* common variables */
  379.     unsigned short type = 0;    /* used by EDEBUG */
  380.     /* variables for 3C509 */
  381.     short status, cst;
  382.     register short rx_fifo;
  383.  
  384.     if(eth_vendor!=VENDOR_3C509)
  385.         return 0;
  386.  
  387.     cst=inw(BASE + EP_STATUS);
  388.  
  389. #ifdef EDEBUG
  390.     if(cst & 0x1FFF)
  391.         printf("-%x-",cst);
  392. #endif
  393.  
  394.     if( (cst & S_RX_COMPLETE)==0 ) {
  395.         /* acknowledge  everything */
  396.         outw(BASE + EP_COMMAND, ACK_INTR| (cst & S_5_INTS));
  397.         outw(BASE + EP_COMMAND, C_INTR_LATCH);
  398.  
  399.         return 0;
  400.     }
  401.  
  402.     status = inw(BASE + EP_W1_RX_STATUS);
  403. #ifdef EDEBUG
  404.     printf("*%x*",status);
  405. #endif
  406.  
  407.     if (status & ERR_RX) {
  408.         outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
  409.         return 0;
  410.     }
  411.  
  412.     rx_fifo = status & RX_BYTES_MASK;
  413.     if (rx_fifo==0)
  414.         return 0;
  415.  
  416.         /* read packet */
  417. #ifdef EDEBUG
  418.     printf("[l=%d",rx_fifo);
  419. #endif
  420.     insw(BASE + EP_W1_RX_PIO_RD_1, packet, rx_fifo / 2);
  421.     if(rx_fifo & 1)
  422.         packet[rx_fifo-1]=inb(BASE + EP_W1_RX_PIO_RD_1);
  423.     packetlen=rx_fifo;
  424.  
  425.     while(1) {
  426.         status = inw(BASE + EP_W1_RX_STATUS);
  427. #ifdef EDEBUG
  428.         printf("*%x*",status);
  429. #endif
  430.         rx_fifo = status & RX_BYTES_MASK;
  431.  
  432.         if(rx_fifo>0) {
  433.             insw(BASE + EP_W1_RX_PIO_RD_1, packet+packetlen, rx_fifo / 2);
  434.             if(rx_fifo & 1)
  435.                 packet[packetlen+rx_fifo-1]=inb(BASE + EP_W1_RX_PIO_RD_1);
  436.             packetlen+=rx_fifo;
  437. #ifdef EDEBUG
  438.             printf("+%d",rx_fifo);
  439. #endif
  440.         }
  441.  
  442.         if(( status & RX_INCOMPLETE )==0) {
  443. #ifdef EDEBUG
  444.             printf("=%d",packetlen);
  445. #endif
  446.             break;
  447.         }
  448.  
  449.         DELAY(1000);
  450.     }
  451.  
  452.     /* acknowledge reception of packet */
  453.     outw(BASE + EP_COMMAND, RX_DISCARD_TOP_PACK);
  454.     while (inw(BASE + EP_STATUS) & S_COMMAND_IN_PROGRESS);
  455. #ifdef EDEBUG
  456.     type = (packet[12]<<8) | packet[13];
  457.     if(packet[0]+packet[1]+packet[2]+packet[3]+packet[4]+
  458.         packet[5] == 0xFF*ETHER_ADDR_SIZE)
  459.         printf(",t=0x%x,b]",type);
  460.     else
  461.         printf(",t=0x%x]",type);
  462. #endif
  463.     return 1;
  464. }
  465.  
  466.  
  467. /*************************************************************************
  468.     3Com 509 - specific routines
  469. **************************************************************************/
  470.  
  471. static int
  472. eeprom_rdy()
  473. {
  474.     int i;
  475.  
  476.     for (i = 0; is_eeprom_busy(IS_BASE) && i < MAX_EEPROMBUSY; i++);
  477.     if (i >= MAX_EEPROMBUSY) {
  478.             /* printf("3c509: eeprom failed to come ready.\r\n"); */
  479.         printf("3c509: eeprom is busy.\r\n"); /* memory in EPROM is tight */
  480.         return (0);
  481.     }
  482.     return (1);
  483. }
  484.  
  485. /*
  486.  * get_e: gets a 16 bits word from the EEPROM. we must have set the window
  487.  * before
  488.  */
  489. static int
  490. get_e(offset)
  491. int offset;
  492. {
  493.     if (!eeprom_rdy())
  494.         return (0xffff);
  495.     outw(IS_BASE + EP_W0_EEPROM_COMMAND, EEPROM_CMD_RD | offset);
  496.     if (!eeprom_rdy())
  497.         return (0xffff);
  498.     return (inw(IS_BASE + EP_W0_EEPROM_DATA));
  499. }
  500.  
  501. static int
  502. send_ID_sequence(port)
  503. int port;
  504. {
  505.     int cx, al;
  506.  
  507.     for (al = 0xff, cx = 0; cx < 255; cx++) {
  508.         outb(port, al);
  509.         al <<= 1;
  510.         if (al & 0x100)
  511.             al ^= 0xcf;
  512.     }
  513.     return (1);
  514. }
  515.  
  516.  
  517. /*
  518.  * We get eeprom data from the id_port given an offset into the eeprom.
  519.  * Basically; after the ID_sequence is sent to all of the cards; they enter
  520.  * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
  521.  * the eeprom data.  We then read the port 16 times and with every read; the
  522.  * cards check for contention (ie: if one card writes a 0 bit and another
  523.  * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
  524.  * compares the data on the bus; if there is a difference then that card goes
  525.  * into ID_WAIT state again). In the meantime; one bit of data is returned in
  526.  * the AX register which is conveniently returned to us by inb().  Hence; we
  527.  * read 16 times getting one bit of data with each read.
  528.  */
  529. static int
  530. get_eeprom_data(id_port, offset)
  531. int id_port;
  532. int offset;
  533. {
  534.     int i, data = 0;
  535.     outb(id_port, 0x80 + offset);
  536.     DELAY(1000);
  537.     for (i = 0; i < 16; i++)
  538.         data = (data << 1) | (inw(id_port) & 1);
  539.     return (data);
  540. }
  541.  
  542. /* a surrogate */
  543.  
  544. void DELAY(val)
  545. {
  546.     int c;
  547.  
  548.     for(c=0; c<val; c+=20) {
  549.         twiddle();
  550.     }
  551. }
  552.  
  553.